import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from ttkbootstrap.scrolled import ScrolledFrame

class ContinueSpider(ttk.Frame):
    def __init__(self,parent, elements):
        super().__init__(parent)
        self.pack(fill=BOTH,expand=YES)

        self.parent = parent
        self.results = []

        self.create_top_area()
        self.create_results_frame()
        
        for element in elements:
            element_txts = element.xpath("text()")
            print(element_txts)
            # self.create_ele_row(element)


    def change_page(self):
        """ 改变页面: 返回到爬虫程序 """
        # 销毁自身容器
        self.destroy()
        # 显示爬虫程序页面
        # SpiderEngine(self.parent)
        s_engine.spider_frame.pack(fill=X, pady=(20, 10))


    def create_top_area(self):
        """ 创建顶部区域: 返回按钮 """
        top_frame = ttk.Frame(
            master=self
        )
        top_frame.pack(fill=X,pady=(20,10))

        return_btn = ttk.Button(
            master=top_frame,
            text="返回",
            bootstyle="primary",
            command=self.change_page,
            width=10
        )
        return_btn.pack(side=LEFT, padx=(30, 0))

        return_label = ttk.Label(
            master=top_frame,
            text="点击返回按钮，返回爬虫程序",
            width=30,
            font=("微软雅黑", 14)
        )
        return_label.pack(side=LEFT, padx=30)

    
    def create_results_frame(self):
        """ 将要查询的元素统一放在此处 """
        self.resultFrame = ScrolledFrame(
            master=self,
            autohide=True,
            height=600,
            padding=(30, 15)
        )
        self.resultFrame.pack(fill=X,pady=10) 


    def create_ele_row(self, element):
        """ 获取其中链接或文本 """
        result_item_row = ttk.Frame(self.resultFrame)
        result_item_row.pack(fill=X, pady=5)

        # 添加进results数组,以待销毁
        self.results.append({"element":result_item_row})
        self.results[-1]["check"] = ttk.IntVar()

        checkbox = ttk.Checkbutton(result_item_row, variable=self.results[-1]["check"])
        checkbox.pack(side=LEFT,ipady=3)

        item_ent = ttk.Entry(result_item_row)
        item_ent.insert(0,element.text)
        item_ent.pack(side=LEFT, fill=X, expand=YES, padx=5, ipady=3)

        text_btn = ttk.Button(
            master=result_item_row, 
            text="文字", 
            width=8,  
            bootstyle=SUCCESS,
            command=lambda: self.clip_to_board(element.text)
        )
        text_btn.pack(side=LEFT, padx=5, ipady=3)

        if hasattr(element, 'tag') and element.tag == "a":
            link_btn = ttk.Button(
                master=result_item_row, 
                text="链接", 
                width=8,  
                bootstyle=INFO,
                command=lambda: self.clip_to_board(element.attrib["href"])
            )
            link_btn.pack(side=LEFT, padx=5, ipady=3)
        else:
            link_btn = ttk.Button(
                master=result_item_row, 
                text="链接", 
                width=8,  
                bootstyle=INFO,
                state="disabled"
            )
            link_btn.pack(side=LEFT, padx=5, ipady=3)


    def clip_to_board(self,clip_text):
        """ 复制到剪贴板中 """
        self.parent.clipboard_clear()
        self.parent.clipboard_append(clip_text)